package edu.washington.ling.syntric.consistency;

import edu.washington.ling.syntric.struct.tree.Token;
import edu.washington.ling.syntric.struct.tree.Tree;

public class LocalMovementRule extends ConsistencyRule {

	private OrderModule orderModule;
	
	public LocalMovementRule(OrderModule orderModule) {
		this.orderModule = orderModule;
	}
	
	@Override
	public void apply(Tree ds) {
		Token dsRoot = ds.getDSRoot();
		apply(dsRoot, ds);
	}
	
	public void apply(Token t, Tree ds) {
		for (Token child: t.getDSDependents()) {
			apply(child, ds);
		}
		for (Token child: t.getDSDependents()) {
			if (orderModule.violatesHeadOrder(child, t, ds)) {
				child.setExtraposed(true);
				int index = ds.nextCoindex();
				Token ec = ConsistencyUtils.createMovementEC("EXTR", child.getDSRelation(), index);
				child.setDSTargetIndex(index);
				ec.setECDependents(child.getECDependents());
				child.removeECDependents();
				t.addECDependent(ec);
			}
			if (orderModule.violatesArgumentOrder(child, t, ds)) {
				child.setScrambledOverArgument(true);
				int index = ds.nextCoindex();
				Token ec = ConsistencyUtils.createMovementEC("SCR", child.getDSRelation(), index);
				child.setDSTargetIndex(index);
				ec.setECDependents(child.getECDependents());
				child.removeECDependents();
				t.addECDependent(ec);
			}
			else if (orderModule.violatesAdjacency(child, t, ds)) {
				child.setScrambledOverModifier(true);
				int index = ds.nextCoindex();
				Token ec = ConsistencyUtils.createMovementEC("SCR", child.getDSRelation(), index);
				child.setDSTargetIndex(index);
				t.addECDependent(ec);
			}
		}
		
	}
}
